<?php
/**
 * @package Autogenerator for Joomla!
 * @version 3.0.0
 * @author Truong Cong Duc <truongcongduc4@gmail.com>
 * @link autogenerator.blogspot.com
 * @copyright (c) 2013, Truong Cong Duc
 * @license GNU General Public License version 2 or later; see LICENSE.txt
 */

defined('_JEXEC') or die;

JFormHelper::loadFieldClass('list');

class JFormFieldTable extends JFormFieldList
{

    public $type = 'Table';

    protected function getOptions()
    {
        static $options = null;

        if (is_array($options))
            return $options;
        
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);

        $query->select('at.id AS value, at.title AS text')
                ->from('#__auto_table AS at')
                ->where('at.component_id IN (SELECT id FROM #__auto_component WHERE status = 1)')
                ->order('at.title ASC');

        $db->setQuery($query);

        try
        {
            $options = $db->loadObjectList();
        }
        catch (RuntimeException $e)
        {
            JError::raiseWarning(500, $e->getMessage);
        }
        
        $parent = new stdClass;
        $parent->text = JText::_('COM_AUTOGENERATOR_MVC_DEFAULT_TABLE_SELECT');
        $parent->value = '';
        array_unshift($options, $parent);

        return $options;
    }

}
